package views.projet.onglet;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;

import main.NotInitializedException;
import models.projet.Phase;
import models.projet.Projet;
import views.projet.onglet.phase.PhaseView;
import controllers.OngletProjetsController;

/**
  * Classe view d'affichage des projets
  
  *
  */
public class ProjetView extends AbstractProjetView
{
	/**
	 *
	 */
	private static final long serialVersionUID = 247560261094586853L;
	private ResumeProjet resumeProjet;
	private AffectationProjet affectesProjet;
	private PhaseView[] phases;

	public ProjetView(OngletProjetsController controller, Projet leProjet) throws NotInitializedException
	{
		super(controller, leProjet);
		
		this.resumeProjet = new ResumeProjet(controller, leProjet); //new JPanel();
		resumeProjet.setBorder(new TitledBorder(null, "R\u00E9sum\u00E9 du projet", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		this.affectesProjet = new AffectationProjet(controller, leProjet);
		/*
		affectesProjet.setLayout(new BorderLayout(0,0));
		affectesProjet.setBackground(new Color(240, 240, 240));
		affectesProjet.setBorder(new TitledBorder(null, "Personnes affect\u00E9es", TitledBorder.LEADING, TitledBorder.TOP, null, null));	
		affectesProjet.add(new EmployesDispoView(controller), BorderLayout.CENTER);
		*/
		
		this.phases = new PhaseView[3];
		
		Phase laPhaseChoisie = null;
		for(Phase laPhase : leProjet.getLesPhases())
			if(laPhase.getId() == Phase.ANALYSE)
				laPhaseChoisie = laPhase;
		
		JPanel analyse = new PhaseView(controller, laPhaseChoisie); //new JPanel();
		this.phases[0] = (PhaseView) analyse;
		
		for(Phase laPhase : leProjet.getLesPhases())
			if(laPhase.getId() == Phase.CONCEPTION)
				laPhaseChoisie = laPhase;

		JPanel conception = new PhaseView(controller, laPhaseChoisie); //new JPanel();
		this.phases[1] = (PhaseView) conception;
		
		for(Phase laPhase : leProjet.getLesPhases())
			if(laPhase.getId() == Phase.REALISATION)
				laPhaseChoisie = laPhase;
		
		JPanel realisation = new PhaseView(controller, laPhaseChoisie); //new JPanel();
		this.phases[2] = (PhaseView) realisation;
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(resumeProjet, GroupLayout.PREFERRED_SIZE, 625, Short.MAX_VALUE)
						.addComponent(affectesProjet, GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(analyse, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(conception, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(realisation, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(resumeProjet, GroupLayout.PREFERRED_SIZE, 241, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(affectesProjet, GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(analyse, GroupLayout.PREFERRED_SIZE, 196, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(conception, GroupLayout.PREFERRED_SIZE, 197, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(realisation, GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)))
					.addGap(176))
		);
		
		setLayout(groupLayout);
		
//		tousLesAffectesTab = new JTable();
//		tousLesAffectesTab.setModel(new DefaultTableModel(
//			new Object[][] {
//				{null, null, null},
//				{null, null, null},
//			},
//			new String[] {
//				"Nom", "Salaire", "Phase"
//			}
//		));
//		affectesProjet.add(tousLesAffectesTab);
	}
	
	public void refresh() throws NotInitializedException
	{
		this.resumeProjet.refresh();
		this.affectesProjet.refresh();
		for(int i=0; i < 3; i++)
			phases[i].refresh();		
	}
}
